var optType = 1, optModuleIndex = 0, optModuleRow = null,
    optMenuIndex = 0, optMenuRow = null, menuData = [],
    authData = [] , menuId = 0, buttonData = [],
    optAuthIndex = 0, optAuthRow = null;

var config = {
    menu:{
        title: '',
        toolbar:'#module-btn',
        height: kit.wh().height - 40,
        onClickRow:function () {
            optMenuRow = component.treegrid.getRowData('#table-menu');
            menuId = optMenuRow.id;
        },
        onDblClickRow:function () {
            opt.auth();
        },
        columns:[
            [
                {title:'菜单名称',field:'name'},
                {field:'url',title:'菜单地址',align:'left'},
                {field:'api',title:'加载api',align:'left'},
                {field:'icon',title:'图标',align:'center',
                    formatter:function (val, row, index) {
                        return '<span class="icon icon16 '+val+'">&nbsp;</span>'
                    }
                },
                {field:'isHidden',title:'显示状态',align:'center',
                    formatter:function (val, row, index) {
                        if(val == 0){
                            return '<span class="icon icon16 icon-accept">&nbsp;</span>';
                        }else{
                            return '<span class="icon icon16 icon-cancel">&nbsp;</span>'
                        }
                    }
                },
                {field:'sort',title:'排序',align:'center'},
                {field:'remark',title:'备注',align:'left'}
            ]
        ]
    },
    module: {
        title: '',
        toolbar:'#menu-btn',
        height: kit.wh().height - 40,
        singleSelect: true,
        pagination: false,
        onClickRow:function () {
            optModuleRow = component.datagrid.getRowData('#table-module');
            optModuleIndex = component.datagrid.getDataRow('#table-module');
            action.menu.get(true);
            optMenuRow = null;
        },
        frozenColumns:[
            [
                {field:'name',title:'模块名称',align:'center'},
                {field:'code',title:'模块编码',align:'center'},
            ]
        ],
        columns:[
            [
                {field:'certified',title:'认证',align:'center',
                    formatter:function (val, row, index) {
                        if(val == 0){
                            return '<span style="color: red;">否</span>';
                        }else{
                            return '<span style="color: green;">是</span>';
                        }
                    }
                },
                {field:'sort',title:'排序',align:'center'},
                {field:'remark',title:'备注',align:'left'}
            ]
        ]
    },
    btn:{
        title: '',
        height: kit.wh().height - 130,
        singleSelect: true,
        pagination: false,
        onDblClickRow:function (rowIndex, rowData) {
            // component.datagrid.deleteRow('#table-btn', rowIndex);
            var data = component.datagrid.all('#table-set-btn');
            var check = false;
            if(data.length > 0){
                for(var i=0;i<data.length;i++){
                    if(data[i].buttonId == rowData.id){
                        check = true;
                        break;
                    }
                }
            }
            if(check){
                kit.alert('已经配置该按钮权限，不需要重复配置。');
                return false;
            }
            rowData['menuId'] = menuId;
            rowData['buttonId'] = rowData.id;
            component.datagrid.append('#table-set-btn', rowData);
        },
        columns:[
            [
                {field:'name',title:'按钮名称',align:'center'},
                {field:'fun',title:'执行函数',align:'center'},
                {field:'method',title:'Method',align:'center',width:120,
                    formatter:function (val, row, index) {
                        var color = '', txt = '';
                        switch (parseInt(val)){
                            case 1:
                                color = '#2c98f0';
                                break;
                            case 2:
                                color = '#83c486';
                                break;
                            case 3:
                                color = '#785549';
                                break;
                            case 4:
                                color = 'red';
                                break;
                        }
                        $.each(format.get2dict('http-method'), function (index, obj) {
                            kit.log(obj)
                            if(obj.code == val){
                                txt = '<span style="color:'+color+'">' + obj.name + '</span>';
                            }
                        })
                        return txt;
                    }
                },
            ]
        ]
    },
    auth:{
        title: '',
        height: kit.wh().height - 130,
        width: (kit.wh().width - ($('body').width() * 0.4) - 20),
        singleSelect: true,
        pagination: false,
        onDblClickRow:function (rowIndex, rowData) {
            component.datagrid.deleteRow('#table-set-btn', rowIndex);
        },
        onClickCell:function (rowIndex, field, value) {

        },
        columns:[
            [
                {field:'name',title:'按钮名称',align:'center'},
                {field:'fun',title:'执行函数',align:'center'},
                {field:'icon',title:'图标',align:'center',
                    formatter:function (val, row, index) {
                        return '<span class="icon icon16 '+val+'">&nbsp;</span>'
                    }
                },
                {field:'method',title:'Method',align:'center',width:120,
                    formatter:function (val, row, index) {
                        var color = '', txt = '';
                        switch (parseInt(val)){
                            case 1:
                                color = '#2c98f0';
                                break;
                            case 2:
                                color = '#83c486';
                                break;
                            case 3:
                                color = '#785549';
                                break;
                            case 4:
                                color = 'red';
                                break;
                        }
                        $.each(format.get2dict('http-method'), function (index, obj) {
                            if(obj.code == val){
                                txt = '<span style="color:'+color+'">' + obj.name + '</span>';
                            }
                        })
                        return txt;
                    }
                },
                {field:'api',title:'API',align:'center', width:180},
                {field:'sort',title:'排序',align:'center'},
                {field:'menuId',title:'menuId',align:'center',hidden:true},
                {field:'buttonId',title:'buttonId',align:'center',hidden:true},
            ]
        ]
    },
    form: {
        title: '',
        width:450,
        height:350,
        buttons: [{
            text:'保 存',
            iconCls:'icon-accept icon16',
            handler:function(){
                switch (optType){
                    case 1:
                        action.module.add();
                        break;
                    case 2:
                        action.module.edit();
                        break;
                    case 4:
                        action.menu.add();
                        break;
                    case 5:
                        action.menu.edit();
                        break;
                }
            }
        },{
            iconCls:'icon-cancel icon16',
            text:'关 闭',
            handler:function(){
                component.dialog.close('#opt-form');
                component.dialog.close('#opt-form-module');
            }
        }]
    },
    icon: {
        title : '选择图标',
        width : kit.wh().width - 10,
        height : kit.wh().height - 10,
        left : 10,
        buttons : [{
            iconCls:'icon-cancel icon16',
            text:'关 闭',
            handler:function(){
                component.dialog.close('#opt-icon');
            }
        }]
    },
    api:{
        title: '配置api地址',
        width:410,
        height:200,
        buttons: [{
            text:'保 存',
            iconCls:'icon-accept icon16',
            handler:function(){
                var api = $('#menu-auth-api').textbox('getValue');
                var sort = $('#menu-auth-sort').textbox('getValue');
                var data = component.datagrid.all('#table-set-btn');
                var row = component.datagrid.getRowData('#table-set-btn');
                row.api = api;
                row.sort = sort == '' ? 0 : sort;
                data = kit.json.update(data, row, "id");
                component.datagrid.load('#table-set-btn', data);
                component.dialog.close('#opt-form-menu-api');
            }
        },{
            iconCls:'icon-cancel icon16',
            text:'关 闭',
            handler:function(){
                component.dialog.close('#opt-form-menu-api');
            }
        }]
    }
}
$(function () {
    component.datagrid.init('#table-module', config.module, false);
    action.module.get();
    action.button.get();
})
var opt = {
    module:{
        add:function (_event) {
            config.form.height = 350;
            config.form.title = '创建系统模块';
            component.dialog.init('#opt-form-module', config.form);
            component.dialog.open('#opt-form-module');
            kit.form.clean('#opt-form-module #opt-form-data');
            optType = 1;
            $('#module-certified').combobox('setValue', 0);
        },
        edit:function (_event) {
            if(optModuleRow == null){
                kit.alert('请先选择一条数据', 'warning');
                return false;
            }
            config.form.height = 350;
            config.form.title = '修改权系统模块';
            component.dialog.init('#opt-form-module', config.form);
            component.dialog.open('#opt-form-module');
            kit.form.set('#opt-form-module #opt-form-data', optModuleRow);
            optType = 2;
        },
        delete: function (_event) {
            if(optModuleRow == null){
                kit.alert('请先选择一条数据', 'warning');
                return false;
            }
            optType = 3;
            var fun = "action.module.delete();";
            var c = {title: '删除系统模块', message: '确定要删除这条数据吗？', submit: fun }
            kit.confirm(c);
        },
    },
    add: function (_event) {
        if(optModuleRow == null){
            kit.alert('请先选择所属的系统模块数据', 'warning');
            return false;
        }
        config.form.title = '新增权限菜单';
        config.form.height = 500;
        component.dialog.init('#opt-form', config.form);
        component.dialog.open('#opt-form');
        kit.form.clean('#opt-form #opt-form-data');
        $('#menu-icon').attr('class', '');
        $('#menu-icon-val').textbox('setValue', '');
        $('#menu-hidden').combobox('setValue', 0);
        optType = 4;
    },
    edit: function (_event) {
        if(optMenuRow == null){
            kit.alert('请先选择一条数据', 'warning');
            return false;
        }
        config.form.title = '编辑权限菜单';
        config.form.height = 500;
        component.dialog.init('#opt-form', config.form);
        component.dialog.open('#opt-form');

        kit.form.set('#opt-form-data', optMenuRow);
        $('#menu-icon').attr('class', 'icon icon16 ' + optMenuRow.icon);
        $('#menu-icon-val').textbox('setValue', optMenuRow.icon);
        kit.render('#button-icon');
        optType = 5;
    },
    delete: function (_event) {
        if(optMenuRow == null){
            kit.alert('请先选择一条数据', 'warning');
            return false;
        }
        var fun = "action.menu.delete();";
        var c = {title: '删除权限菜单', message: '确定要删除这个权限菜单吗？', submit: fun }
        kit.confirm(c);
    },
    icon: function (_event) {
        component.dialog.init('#opt-icon', config.icon);
        component.dialog.open('#opt-icon');
    },
    getIcon: function (_event) {
        var icon = $(_event).attr('class');
        $('#menu-icon').attr('class', 'icon icon16 '+ icon);
        $('#menu-icon-val').textbox('setValue', icon);
        component.dialog.close('#opt-icon');
    },
    auth:function () {
        if(optMenuRow == null){
            kit.alert('请先选择一个菜单', 'warning');
            return false;
        }
        if(optMenuRow.parentId == 0){
            kit.alert('顶级菜单不能配置权限按钮', 'warning');
            return false;
        }
        config.icon.buttons= [{
            text:'保 存',
            iconCls:'icon-accept icon16',
            handler:function(){
                var data = component.datagrid.all('#table-set-btn');
                action.button.auth(data);
            }
        },{
            iconCls:'icon-cancel icon16',
            text:'关 闭',
            handler:function(){
                component.dialog.close('#opt-form-set-auth');
            }
        }]
        component.dialog.init('#opt-form-set-auth', config.icon);
        component.dialog.open('#opt-form-set-auth');
        config.btn.data = buttonData;
        component.datagrid.init('#table-btn', config.btn, false);
        action.button.init();
    },
    updateAuth:function (_event) {
        optAuthRow = component.datagrid.getRowData('#table-set-btn');
        optAuthIndex = component.datagrid.getDataRow('#table-set-btn');
        if(optAuthRow == null){
            kit.alert('请先选择一个权限按钮', 'warning');
            return false;
        }
        component.dialog.init('#opt-form-menu-api', config.api);
        component.dialog.open('#opt-form-menu-api');
        $('#menu-auth-api').textbox('setValue', optAuthRow.api);
        $('#menu-auth-sort').textbox('setValue', optAuthRow.sort);
    }
}
var action = {
    menu:{
        get: function (_flag) {
            var param = {
                url: constant.route.menu + '/' + optModuleRow.id
            }
            var request = http.get(param);
            request.done(function (d) {
                var data = d.result;
                config.menu.data = data;
                menuData = [];
                menuData.push({id:0, name:'顶级菜单'});
                menuData = menuData.concat(data);
                if(_flag){
                    component.treegrid.init('#table-menu', config.menu, false);
                }
                $('#menu-parentId').combobox({
                    data: menuData,
                    valueField:'id',
                    textField:'name'
                });
                $('#menu-parentId').combobox('setValue', 0);
            })
        },
        add:function () {
            var data = kit.form.get('#opt-form #opt-form-data');
            data['icon'] = $('#menu-icon-val').textbox('getValue');
            data['moduleId'] = optModuleRow.id;
            var param = {
                url : constant.route.menu,
                data : data
            }
            var request = http.post(param);
            request.done(function (d) {
                var data = d.result;
                kit.alert(d.message, 'success');
                component.treegrid.append('#table-menu', data);
                kit.form.clean('#opt-form #opt-form-data');
                $('#menu-icon').attr('class', '');
                $('#menu-icon-val').textbox('setValue', '');
                $('#menu-hidden').combobox('setValue', 0);

                action.menu.get();
            })
        },
        edit:function () {
            var data = kit.form.get('#opt-form-data');
            data['id'] = optMenuRow.id;
            data['icon'] = $('#menu-icon-val').textbox('getValue');
            var param = {
                url: constant.route.menu,
                data: data
            }
            var request = http.put(param);
            request.done(function (d) {
                var data = d.result;
                kit.alert(d.message, 'success');
                component.treegrid.updateRow('#table-menu', data);
                component.dialog.close('#opt-form');
            })
        },
        delete:function () {
            var param = {
                url: constant.route.menu + '/' + optMenuRow.id
            }
            var request = http.delete(param);
            request.done(function (d) {
                kit.alert(d.message, 'success');
                component.treegrid.delete('#table-menu', optMenuRow.id);
                optMenuRow = null;
            })
        },
    },
    module:{
        get: function () {
            var param = {
                url: constant.route.module
            }
            var request = http.get(param);
            request.done(function (d) {
                var data = d.result;
                component.datagrid.load('#table-module', data);
            })
        },
        add:function () {
            var data = kit.form.get('#opt-form-module #opt-form-data');
            var param = {
                url : constant.route.module,
                data : data
            }
            var request = http.post(param);
            request.done(function (d) {
                var data = d.result;
                kit.form.clean('#opt-form-module #opt-form-data');
                kit.alert(d.message, 'success');
                $('#module-certified').combobox('setValue', 0);
                component.datagrid.append('#table-module', data);
            })
        },
        edit:function () {
            var data = kit.form.get('#opt-form-module #opt-form-data');
            data['id'] = optModuleRow.id;
            var param = {
                url: constant.route.module,
                data: data
            }
            var request = http.put(param);
            request.done(function (d) {
                var data = d.result;
                kit.alert(d.message, 'success');
                component.datagrid.updateRow('#table-module', optModuleIndex, data);
                component.dialog.close('#opt-form-module');
            })
        },
        delete:function () {
            var param = {
                url: constant.route.module + '/' + optModuleRow.id
            }
            var request = http.delete(param);
            request.done(function (d) {
                kit.alert(d.message, 'success');
                component.datagrid.deleteRow('#table-module', optModuleIndex);
                optModuleRow = null;
            })
        },
    },
    button:{
        init: function () {
            var param = {
                url: constant.route.menu + '/auth/' + menuId
            }
            var request = http.get(param);
            request.done(function (d) {
                config.auth.data = d.result;
                component.datagrid.init('#table-set-btn', config.auth, false);
            })
        },
        get: function () {
            var param = {
                url: constant.route.button
            }
            var request = http.get(param);
            request.done(function (d) {
                buttonData = d.result.rows;
            })
        },
        auth:function (_data) {
            var param = {
                url : constant.route.menu + '/auth/' + menuId,
                data : _data
            }
            var request = http.put(param);
            request.done(function (d) {
                kit.alert(d.message, 'success');
                component.dialog.close('#opt-form-set-auth');
            })
        }
    },
}